package com.js.basicutils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceNetWorkUtil {

	public SharedPreferences mConfigPrefs ; // 配置存储
	public static final String PREFERENCE_CONFIG_FILE_NAME = "ip_port_config";
	public static final String PREFERENCE_CONFIG_SERVER_ADDRESS_SERVER_IP = "address_server_ip";
	public static final String PREFERENCE_CONFIG_SERVER_ADDRESS_PORT = "address_server_port";

	public SharedPreferences getmConfigPrefs(Context ctx) {
		mConfigPrefs = ctx.getSharedPreferences(PREFERENCE_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
		return mConfigPrefs;
	}


	// -------------------config
	public static String getServerIP(Context ctx) {
		SharedPreferences sp = ctx.getSharedPreferences(PREFERENCE_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
		return sp.getString(PREFERENCE_CONFIG_SERVER_ADDRESS_SERVER_IP, "218.76.24.159");
	}

	public static String getServerPort(Context ctx) {
		SharedPreferences sp = ctx.getSharedPreferences(PREFERENCE_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
		return sp.getString(PREFERENCE_CONFIG_SERVER_ADDRESS_PORT, "9014");
	}

	public static boolean saveNetInfo(Context ctx, String serverIP, String serverPort) {
		SharedPreferences mSP = ctx.getSharedPreferences(PREFERENCE_CONFIG_FILE_NAME, Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = mSP.edit();
		editor.putString(PREFERENCE_CONFIG_SERVER_ADDRESS_SERVER_IP, serverIP);
		editor.putString(PREFERENCE_CONFIG_SERVER_ADDRESS_PORT, serverPort);
		return editor.commit();
	}

	public static String getServerPortIP(Context context) {
		String serverIP = getServerIP(context);
		String serverPort = getServerPort(context);
		return "http://" + serverIP + ":" + serverPort;
	}
}
